package com.jfsnpm.jfsnpm.plugin.snakerflow;

import org.snaker.engine.SnakerInterceptor;
import org.snaker.engine.core.Execution;
import org.snaker.engine.entity.Task;

import com.jfsnpm.jfsnpm.core.AppConfig;
import com.jfsnpm.jfsnpm.core.dao.User;
import com.jfsnpm.jfsnpm.core.service.UserService;
import com.jfsnpm.jfsnpm.core.util.MessageHelper;

/**
* 通知拦截器
*/
public class NoticeInterceptor implements SnakerInterceptor {
	/**
	 * 拦截产生的任务对象，打印日志
	 */
	public void intercept(Execution execution) {
			
			for(Task task : execution.getTasks()) {
				String tos="";
				String content="<html>";
				content+="新任务下发通知<br>";
				content+="任务名称："+task.getDisplayName()+"<br>";
				content+="参与者：";
				StringBuffer buffer = new StringBuffer(100);
				buffer.append("任务标识=").append(task.getId());
				buffer.append(",名称=[").append(task.getDisplayName())
					.append("|").append(AppConfig.properties.getProperty("site")+AppConfig.rootpath+"/Api/viewTask/?"
						+ "taskId="+task.getId()).append("]");
				buffer.append(",创建时间=").append(task.getCreateTime());
				buffer.append(",参与者={");
				if(task.getActorIds() != null) {
					for(String actor : task.getActorIds()) {
						if(tos.equals("")){
							tos = actor;
						}else{
							tos = tos + ";"+ actor;
						}
						User user = UserService.getUserById(actor);
						buffer.append(user.getUserName()).append(";");
						content+=user.getUserName();
					}
				}
				buffer.append("}");
				
				content+="<br>";
				content+="<a href='"+AppConfig.properties.getProperty("site")+AppConfig.rootpath+"/Api/viewTask/?"
						+ "taskId="+task.getId()+"'>处理任务</a>";
				content+="</html>";
				//MessageHelper.sendNotify("任务通知", tos, buffer.toString());
				//MessageHelper.sendMail("任务下发通知", tos, content);
			}
	}
}

